$("<link>").attr({ 
	rel: "stylesheet",
    type: "text/css",
    href: "/Public/Home/js/win/style.css?v=4"
}).appendTo("head");

$(".win").click(function(){
	$(this).win();
	return false;
});
/**
 * 触发HTML标签的Ajax请求事件，并将请求返回内容在新弹出的窗口进行显示
 * 调用方法如：$(tag).win(callback)或$.win(tab,callback)
 * 其中ajax的url默认为href->rel->alt->action->当前url
 */
jQuery.extend({
	//浏览器宽度
	winWidth : 0,
	
	//浏览器高度
	winHeight : 0,

	//是否全屏
	win_is_full_screen : false,
	
	winHTML : "<div id='winCoverBackBg'></div>"+
		"<div id='winFrontContenter'>" +
			"<div class='winTitleBar' style='cursor: pointer;'><span></span><a title='关闭'>×</a></div>" +
			"<div id='winBodyContainer'></div>" +
		"</div>",
	
	//相关初始化：获取窗口宽高
	winInit : function(){
		jQuery.winWidth = jQuery(window).width();
		jQuery.winHeight = jQuery(window).height();
		$('body').css({'overflow':'hidden'});
		$.win_is_full_screen = false;
	},
	
	//添加一个完全透明的DIV层覆盖整个界面
	winPostLoad : function(){
		$("body").append($.winHTML);
		$("#winCoverBackBg").height($(document).height()).width("100%");
		if($.browser.msie && $.browser.version=='6.0')
			$("select").hide();
		$("#winFrontContenter .winTitleBar a").bind("click",function(){
			$("#winCoverBackBg").remove();
			$("#winFrontContenter").remove();
			$('body').css({'overflow':'auto'});
		});
	},
	
	//计算弹出框口的宽度，并将窗口调整为居中
	winCalFront : function(){
		$wfc = $("#winFrontContenter");
		$wfc.removeAttr('style');
		var win_width  = $wfc.innerWidth();
		var win_height = $wfc.innerHeight();

		var is_width_out_win  = win_width > $(window).width();
		var is_height_out_win = win_height > $(window).height();

		win_width = is_width_out_win ? $(window).width() : win_width;
		win_height = is_height_out_win ? $(window).height() : win_height;

		//是否全屏
		if($.win_is_full_screen){
			$wfc.width($(window).width());
			win_width = $(window).width();
			$('#winBodyContainer').height($(window).height());
			win_height = $(window).height();
			
		}

		if(is_width_out_win){
			$('#winBodyContainer').css('overflow-x', 'scroll');
		}
		if(is_height_out_win){
			$('#winBodyContainer').css('overflow-y', 'scroll');
			$('#winBodyContainer').height(win_height-30);
		}
		
		$wfc.width(win_width);
		$wfc.css({
			left:(($.winWidth - win_width) / 2) + "px",
			top:(($.winHeight - win_height) / 2) + "px"
		});

		$.win_is_full_screen = false;
		// $wfc.width($wfc.outerWidth()+16);
	},
	
	//将请求返回的内容填充到该窗口中
	winPut : function(html,title,callback){
		$wbc = $("#winBodyContainer");
		$wbc.html(html);
		$wbc.find("a").each(function(){
			if($(this).hasClass("noneAjax")) return ;
			$(this).bind("click",function(){
				return $.winBindEvent(this,callback);
			});
		});
		$wbc.find("form:not(.filter)").bind("submit",function(){
			var data = $(this).serialize();
			return $.winBindEvent(this,callback,data);
		});
		if(title && typeof(title) != "undefined"){
			$("#winFrontContenter .winTitleBar span").html(title);
		}
		$.winCalFront();
		//鼠标在标题栏上，启用拖动功能
		$( "#winFrontContenter .winTitleBar").live('mousemove',function () {
			$('#winFrontContenter').draggable();
		});
		//鼠标离开标题栏上，取消拖动功能
		$( "#winFrontContenter .winTitleBar").live('mouseout',function () {
			$('#winFrontContenter').draggable("destroy");
		});
	},
	
	//将新加载进来的内容中的“a”、“form”绑定事件，以便新请求内容在该窗口进行显示
	winBindEvent : function(tag,callback,data){
		data = data ? data : '';
		var url        = $(tag).attr("href") ? $(tag).attr("href") : ($(tag).attr("rel") ? $(tag).attr("rel") : $(tag).attr("url"));
		var url        = url ? url : $(tag).attr("action");
		var method     = $(tag).attr("method");
		var url        = url ? url : window.location.href;
		var title      = $(tag).attr("title");
		var is_animate = $(tag).attr("is-animate");
		if(!method && !$(tag).parent().hasClass('page')){
			title = title ? title : $(tag).text();
		}
		if(method){
			btn_title = $(tag).find('input[type="submit"]').attr('title');
			title = btn_title ? btn_title : title;
		}
		title = title ? title : $('.winTitleBar span').text();

		is_animate = is_animate?true:false;
		loading();
		$.ajax({
			url : url,
			data : data,
			type : method=='post' ? 'post' : 'get',
			dataType:'html',
			success : function(requestText){
				removeLoading();
				//判断返回值不是 json 格式
				if (!requestText.match("^\{(.+:.+,*){1,}\}$")){
					$.winRemove();
					if($("#winFrontContenter").length<=0) $.winPostLoad();
					$.winPut(requestText, title, callback, is_animate);
					if(callback)eval(callback);
					$(".removeWin").click(function(){
						$.winRemove();
					});
				}else{
					json = jQuery.parseJSON(requestText);
					if(json.status == 1){
						msgbox(json.info);
						if(json.url){
							jumpurl(json.url);
						}
						var node_id      = json.data.node_id;
						var node_data    = json.data.node_data;
						var del_node_id  = json.data.del_node_id;
						var no_close_win = json.data.no_close_win;
						if(node_id && node_data){
							$.each(node_data , function(key, value){
								$('.'+node_id).find('.'+key).html(value);
							});
						}
						if(del_node_id){
							$('.'+del_node_id).remove();
						}
						if(no_close_win == true){
							return false;
						}
						//关闭窗口
						$.winRemove();
					}else{
						msgbox(json.info,'error');
					}
				}
				//$.winCalFront();
			}
		});
		return false;
	},
	
	//移除新弹出的窗口
	winRemove : function(){
		$("#winFrontContenter .winTitleBar a").trigger("click");
	},
	
	win : function(tag,callback){
		$.winInit();
		var is_full_screen   = $(tag).attr("full-screen");
		var is_full_screen   = is_full_screen ? true : false;
		$.win_is_full_screen = is_full_screen;
		return $.winBindEvent(tag,callback);
	},
	win_info : function(tag,requestText,title, is_full_screen){
		$.winInit();
		var title = title ? title : $(tag).attr("title");
		var is_full_screen = is_full_screen ? is_full_screen : $(tag).attr("full-screen");
		var is_full_screen = is_full_screen ? true : false;
		$.win_is_full_screen = is_full_screen;
		$.winPostLoad();
		$.winPut(requestText,title);
		$(".removeWin").click(function(){
			$("#winCoverBackBg").remove();
			$("#winFrontContenter").remove();
			$('body').css({'overflow':'auto'});
		});
	}
});
jQuery.fn.extend({
	win : function(callback){
		return jQuery.win(this,callback);
	},
	win_info : function(html,title){
		return jQuery.win_info(this,html,title);
	},
	winRemove : function(){
		$("#winCoverBackBg").remove();
		$("#winFrontContenter").remove();
		$('body').css({'overflow':'auto'});
	}
});

function ajax_win (url, data, method, title) {
	var title = title ? title : '提示信息';
	loading();
	$.ajax({
		url : url,
		data : data,
		type : method == 'post' ? 'post' : 'get',
		dataType:'html',
		success : function(data){
			removeLoading();
			//判断返回值不是 json 格式
			if (!data.match("^\{(.+:.+,*){1,}\}$")){
				$.win_info('', data, title);
			}else{
				json = jQuery.parseJSON(data);
				if(json.status == 1){
					msgbox(json.info);
				}else{
					msgbox(json.info, 'error');
				}
				//有返回链接，则跳转
				if(json.url){
					jumpurl(json.url);
				}
			}
		}
	});
	return false;
}